/**
 * This file contains javascript to handle information of medical agent.
 */
//////////////////////// LOADING A PAGE ////////////
function loadPage(sourceDiv, actionURL, serviceId) {        
    $.ajax ({       
        url:        actionURL + "?set_ajax_view", 
        data:		"service_id=" + serviceId,	             
        dataType:   "html",
        beforeSend: function() {                    
            $('div#listContent').hide(); 
            $('div#pageContent').hide("slow");                
        },
        complete:   function() {
            $('div#' + sourceDiv).show("fast");         
        },
        success:  function(data) {
            $("div#" + sourceDiv).html(data);          
        }
    });
}

function Edit(id) {
    $('div#listContent').hide();
    loadPage('pageContent', 'Edit', id);
}
 
function Save() {
    if (!isNotEmpty($('#name'), _("Empty name"), "status_message")) {
        return;
    }
    
    var submitData = $('#add_form').formSerialize();
        
    $.ajax ({
            type:   "POST",
            url:    "SaveAjax?set_ajax_view",
            data:   submitData,
            beforeSend: function() {
                $('#btn_save').hide();
                $('#barLoading').show();                
            },
            complete:   function() {
                $('#btn_save').show();
                $('#barLoading').hide();
            },
            success:    function(data) {            	
                $('div#pageContent').hide('slow');
                loadPage('listContent', 'List');
                $('div#listContent').show();
            }
        });
    
}

function changeStatus(id, status) {	
	var message = _('Are you sure to delete this career?');
	if(status == 'unlock'){
		message = _('Reuse this career?');
	}
	promptConfirm(_("Delete"),message, function() {		
		submitChangeStatus(id, status); 
		return true;
	},'YesNo','warn',_('No'));	
		
} 

function submitChangeStatus(id, status) {
	$.ajax ({
		type: 	"GET",
		url:	"ChangeStatusAjax?set_ajax_view",
		data:	{'id' : id, 'status' : status},
		success: function(data) {
			var careerTable = $('#list');			
			var html = '';
			var changedStatus = '';
			if(status == 'lock'){
				changedStatus = '<span class="impression">'+ _("Temporary delete")+ '</span>';
				html = '<a class="btn" href="javascript:changeStatus(' + id + ',\'unlock\');" title="'+ _("Unlock career")+ '">'
                	+ '<img src="../images/unlock_small.png" alt="'+ _("Unlock")+ '"></img></a>';
			}
			else if(status == 'unlock'){
				changedStatus = '<span>'+ _("In Use")+ '</span>';
				html = '<a class="btn" href="javascript:changeStatus(' + id + ',\'lock\');" title="'+ _("Delete career")+ '">'
                	+ '<img src="../images/lock_small.png" alt="'+ _("Temporary delete")+ '"></img></a>';
			}
			$('tr#' + id + ' td:nth-child(2)' + careerTable).html(changedStatus);
			$('tr#' + id + ' td:nth-child(3)' + careerTable).html(html);
		}
	});
}
